package com.hit.aircraftwar.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.Button;

import com.hit.aircraftwar.R;
import com.hit.aircraftwar.setter.ParameterSet;
import com.hit.aircraftwar.setter.TimeController;

import java.util.Objects;

/**
 * 此为游戏初始界面，可供跳转到其他界面
 */
public class MainActivity extends AppCompatActivity {

    public static ParameterSet parameterSet = ParameterSet.INSTANCE;
    private Button startButton;
    private Button mallButton;
    private Button settingButton;
    private Button exitButton;
    private Button onlineButton;

    public static int width;
    public static int height;
    @SuppressLint("StaticFieldLeak")
    public static MainActivity mainActivity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Objects.requireNonNull(getSupportActionBar()).hide();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainActivity = this;

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;
        height = dm.heightPixels;


        startButton = findViewById(R.id.startButton);
        mallButton = findViewById(R.id.mallButton);
        settingButton = findViewById(R.id.settingButton);
        exitButton = findViewById(R.id.exitButton);
        onlineButton = findViewById(R.id.onlineButton);

        onlineButton.setOnClickListener(view -> {
            if("Visitor".equals(ParameterSet.INSTANCE.getName())){
                showDialog();
            } else {
                Intent intent = new Intent(MainActivity.this, RoomActivity.class);
                startActivity(intent);
            }
        });
        startButton.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, StartActivity.class);
            startActivity(intent);
        });
        mallButton.setOnClickListener(view -> {
            ParameterSet.INSTANCE.setPoint();
            TimeController t = new TimeController(300, 0 ,0);
            Thread th = new Thread(t);
            th.start();
            try {
                th.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Intent intent = new Intent(MainActivity.this, MallActivity.class);
            startActivity(intent);
        });
        settingButton.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, SettingActivity.class);
            startActivity(intent);
        });
        exitButton.setOnClickListener(view -> System.exit(0));
    }

    private void showDialog(){
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        normalDialog.setTitle("");
        normalDialog.setMessage("游客模式下无法在线游戏！");
        normalDialog.setPositiveButton("登录",
                (dialog, which) -> {
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);
                });
        normalDialog.setNegativeButton("取消",
            (dialog, which) -> {

                });
        // 显示
        normalDialog.show();
    }

}